// Name: ValidateSearchForm.js
//
// The method is called when the "submit" button is pressed
// It dynamically figures out where the "from" "to" radio
// button is selected and validates whether the "submit"
// should proceed or to issue an alert if the selections
// are invalid.
//
// Unfortunately the names and values of the radio buttons
// are hard-coded here, to match those in /cgi-bin/search.pl
//
// @param  form    an instance of the form object
// @return boolean true/false
//                 true continues the submit process
//                 false does not proceed with the submit
//
//

function validateSearchForm(form) {

    var i, index, startYear, endYear;
    var needToCheck   = "false";
    var NORESTRICTION = "No Restriction...";

    for (i=0; i < form.elements.length; i++) {

        if ( (form.elements[i].name == "daterange") &&
                (form.elements[i].value == "yearrange") &&
                (form.elements[i].checked == true) ) {
            needToCheck = "true";
            break;
        } 
    }
 
    if ( needToCheck == "true" ) {
        for (i=0; i < form.elements.length; i++) {

            if ( form.elements[i].name == "fromyear" ) {
                index     = form.elements[i].selectedIndex;
                startYear = form.elements[i].options[index].text;
            }
            if ( form.elements[i].name == "toyear" ) {
                index   = form.elements[i].selectedIndex;
                endYear = form.elements[i].options[index].text;
            } 
        }

        if ((startYear == NORESTRICTION) && (endYear == NORESTRICTION)) {

            return true;

        } else {
            if (startYear == NORESTRICTION) {
                alert("Invalid StartYear: <"+startYear+
                       "> End Year: <"+endYear+">");
                return false;
            } 
            if (endYear == NORESTRICTION) {
                alert("Invalid StartYear: <"+startYear+
                        "> EndYear: <"+endYear+">");
                return false;
            } 
            if ( startYear > endYear ) {
                alert("From: <"+startYear+
                         "> must be LESS THAN or EQUAL "+
                         " To: <"+endYear+">");
                return false;
            }
        }
    }
    return true;
}
